var config = require('../../../utils/config.js')
var util = require('../../../utils/util.js')
var API_URL = config.API_URL
var userInfo;
var upload_P = util.wxPromise(my.uploadFile);
var request_P = util.wxPromise(my.httpRequest);
var patt = new RegExp("myfile://");
var upload_img = (img) => {
    if (patt.test(img)) {
        console.log('上传图片');
        return upload_P({
            url: API_URL.upload_img,
            filePath: img,
            fileName: 'upload_file',
            fileType:"image"
        })
    } else {
        console.log('网络图片');
        return new Promise((resolve, reject) => {
            var res = {};
            res.data = img;
            console.log(res);
            resolve(res);
        })
    }
}
Page({
    data: {
        status: [
            {
                name: '填写资料',
                text_class: 'black',
                dot_class: 'dot-choose'
            },
            {
                name: '支付认证金',
                text_class: '',
                dot_class: 'dot'
            },
            {
                name: '审核结果',
                text_class: '',
                dot_class: 'dot'
            },
        ],
        stepTemplate: "step1",
        info: {
            real_name: "",
            id_card_number: "",
            id_card_fore: "",
            id_card_back: "",
            id_card_hand: "",
        }
    },
    onLoad: function (options) {
        var that = this;
        // 拿到用户的ID
        userInfo = my.getStorageSync({ key: 'user' }).data;
        my.httpRequest({
            url: API_URL.account_status,
            data: {
                user_id: userInfo.userId
            },
            method: 'POST',
            success: function (res) {
                console.log(res)
                var identification = res.data.data.identification;
                console.log(identification)
                if (!identification) {
                    identification = 0;
                }
                var certificationPaymentStatus = res.data.data.certificationPaymentStatus;
                var status = that.data.status;
                var stepTemplate, resultTemplate;
                switch (identification) {
                    case 0: stepTemplate = 'step1'; break;
                    case 1: status[0].text_class = ''; status[0].dot_class = 'dot'; status[2].text_class = 'black'; status[2].dot_class = 'dot-choose'; stepTemplate = 'step3'; resultTemplate = 'success'; break;
                    case -1: status[0].text_class = ''; status[0].dot_class = 'dot'; status[2].text_class = 'black'; status[2].dot_class = 'dot-choose'; stepTemplate = 'step3'; resultTemplate = 'edit'; break;
                    // case 2: if (certificationPaymentStatus == 1) { status[0].text_class = ''; status[0].dot_class = 'dot'; status[2].text_class = 'black'; status[2].dot_class = 'dot-choose'; stepTemplate = 'step3'; resultTemplate = 'waiting'; } else { status[0].text_class = ''; status[0].dot_class = 'dot'; status[1].text_class = 'black'; status[1].dot_class = 'dot-choose'; stepTemplate = 'step2'; util.POST(API_URL.certification_batch, { userId: userInfo.userId }).then(res => { that.setData({ batchNumber: res.data.batchNumber }) }).catch(res => { console.log(res) }) } break;
                }
                that.setData({
                    status: status,
                    stepTemplate: stepTemplate,
                    resultTemplate: resultTemplate
                })
            },
            fail: function (res) { },
            complete: function (res) { },
        })
        my.httpRequest({
            url: API_URL.get_certification,
            data: {
                userId: userInfo.userId
            },
            method: 'POST',
            success: function (res) {
                console.log(res)
                if (!res.data.data) {
                    return
                }
                let info = res.data.data;
                // 时间格式化
                info.date = util.formatTime(info.pay_time);
                that.setData({
                    info: res.data.data
                })
            },
            fail: function (res) { },
            complete: function (res) { },
        })
    },
    // 支付
    pay: function (e) {
        var that = this;
        var status = that.data.status;
        var stepTemplate = 'step3';
        var resultTemplate = 'waiting';
        that.setData({
            status: status,
            stepTemplate: stepTemplate,
            resultTemplate: resultTemplate
        })
    },
    // 返回个人中心
    goToUser: function (e) {
        my.redirectTo({
            url: '/pages/user/user',
        })
    },
    // 保存输入的姓名
    setName: function (e) {
        let info = this.data.info;
        info.real_name = e.detail.value;
        this.setData({
            info: info
        })
    },
    // 保存输入的身份证
    setCard: function (e) {
        let info = this.data.info;
        info.id_card_number = e.detail.value;
        this.setData({
            info: info
        })
    },
    // 身份证上传
    cardFore: function (e) {
        var that = this;
        var info = that.data.info;
        my.chooseImage({
            count: 1,
            success: function (res) {
                info.id_card_fore = res.apFilePaths[0];
                that.setData({
                    info: info
                })
            }
        })
    },
    // 身份证上传
    cardBack: function (e) {
        var that = this;
        var info = that.data.info;
        my.chooseImage({
            count: 1,
            success: function (res) {
                // 给info添加信息
                info.id_card_back = res.apFilePaths[0];
                that.setData({
                    info: info
                })
            }
        })
    },
    // 身份证上传
    cardHand: function (e) {
        var that = this;
        var info = that.data.info;
        my.chooseImage({
            count: 1,
            success: function (res) {
                info.id_card_hand = res.apFilePaths[0];
                that.setData({
                    info: info
                })
            }
        })
    },
    // 申请
    IdCard: function (e) {
        console.log(e);
        var that = this;
        var name = e.detail.value.name;
        var idcard = e.detail.value.idcard;
        var cardFore = that.data.info.id_card_fore;
        var cardBack = that.data.info.id_card_back;
        var cardHand = that.data.info.id_card_hand;
        var cardFore_url, cardBack_url, cardHand_url;
        var status = that.data.status;
        var res = {};
        if (name == '') {
            util.showNotice('请输入姓名');
            return;
        }
        if (!util.checkIDcard(idcard)) {
            util.showNotice('请输入正确的身份证号');
            return;
        }
        if (idcard.length != 18) {
            util.showNotice('请输入18位身份证号');
            return;
        }
        if (cardFore == "") {
            util.showNotice('请上传身份证正面照片');
            return;
        }
        if (cardBack == "") {
            util.showNotice('请上传身份证背面照片');
            return;
        }
        if (cardHand == "") {
            util.showNotice('请上传手持身份证照片');
            return;
        }
       my.showLoading({
            content: '上传图片中'
        })
        upload_img(cardFore)
            .then((res) => {
                cardFore_url = res.data;
                console.log(cardFore_url);
                return upload_img(cardBack)
            }).then((res) => {
                cardBack_url = res.data;
                console.log(cardBack_url);
                return upload_img(cardHand)
            }).then((res) => {
                cardHand_url = res.data;
                console.log(cardHand_url);
                return request_P({
                    url: API_URL.certification,
                    data: {
                        userId: userInfo.userId,
                        cardFore: cardFore_url,
                        cardBack: cardBack_url,
                        cardHand: cardHand_url,
                        realName: name,
                        idNumber: idcard,
                    },
                    method: 'POST'
                })
            }).then((res) => {
                my.hideLoading();
                if (that.data.stepTemplate == 'step1') {
                    status[0].text_class = '';
                    status[0].dot_class = 'dot';
                    status[1].text_class = 'black';
                    status[1].dot_class = 'dot-choose';
                    var stepTemplate, resultTemplate;
                    stepTemplate = 'step2';
                } else {
                    var stepTemplate, resultTemplate;
                    stepTemplate = 'step3';
                    resultTemplate = 'waiting';
                }
                that.setData({
                    status: status,
                    stepTemplate: stepTemplate,
                    resultTemplate: resultTemplate,
                    batchNumber: res.data.batchNumber
                })
            }).catch((err) => { console.log(err); my.hideLoading(); util.showNotice('网络错误') });
    },
})